using RaceHFTools.BluetoothLE.Bean;
using System;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace RaceHFTools.Pages.Tweak
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TweakGsensorPage : ContentPage
    {
        private readonly Bean bean;

        public TweakGsensorPage(Bean bean)
        {
            InitializeComponent();

            this.bean = bean;
        }

        #region 事件处理
        bool rate_set_done;

        void OnAllSetDone()
        {
            if (rate_set_done)
            {
                Device.BeginInvokeOnMainThread(async () => await DisplayAlert("加速度计设置", "设置完毕", "确定"));
            }
        }

        void ResetAllFlags()
        {
            rate_set_done = false;
        }

        void OnRateGet(uint rate)
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                switch (rate)
                {
                    case 1: cbxGsensor1Hz.IsChecked = true; break;
                    case 5: cbxGsensor5Hz.IsChecked = true; break;
                    case 10: cbxGsensor10Hz.IsChecked = true; break;
                    case 20: cbxGsensor20Hz.IsChecked = true; break;
                    case 50: cbxGsensor50Hz.IsChecked = true; break;
                    case 100: cbxGsensor100Hz.IsChecked = true; break;
                }
            });
        }

        void OnRateSet(bool success)
        {
            rate_set_done = success;
            OnAllSetDone();
        }

        async Task<bool> DoRateSet()
        {
            uint rate = 0;

            if (cbxGsensor1Hz.IsChecked) rate = 1;
            else if (cbxGsensor5Hz.IsChecked) rate = 5;
            else if (cbxGsensor10Hz.IsChecked) rate = 10;
            else if (cbxGsensor20Hz.IsChecked) rate = 20;
            else if (cbxGsensor50Hz.IsChecked) rate = 50;
            else if (cbxGsensor100Hz.IsChecked) rate = 100;

            if (rate == 0)
            {
                Device.BeginInvokeOnMainThread(async () => await DisplayAlert("加速度计", "请选择刷新率", "确定"));
                return false;
            }

            await bean.Tweak.SetGsensorRate(rate);

            return true;
        }
        #endregion

        #region OnAppearing/OnDisappearing
        protected override void OnAppearing()
        {
            base.OnAppearing();

            bean.Tweak.GsensorGetRateEvent += OnRateGet;
            bean.Tweak.GsensorSetRateEvent += OnRateSet;

            _ = bean.Tweak.GetGsensorAll();
        }

        protected override void OnDisappearing()
        {
            bean.Tweak.GsensorGetRateEvent -= OnRateGet;
            bean.Tweak.GsensorSetRateEvent -= OnRateSet;

            base.OnDisappearing();
        }
        #endregion

        #region 交互事件
        void CheckBox_GsensorRate_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            CheckBox cbx = sender as CheckBox;
            CheckBox[] cbxs = new CheckBox[] { cbxGsensor1Hz, cbxGsensor5Hz, cbxGsensor10Hz, cbxGsensor20Hz, cbxGsensor50Hz, cbxGsensor100Hz };
            foreach(CheckBox c in cbxs)
            {
                c.CheckedChanged -= CheckBox_GsensorRate_CheckedChanged;
                c.IsChecked = c == cbx;
                c.CheckedChanged += CheckBox_GsensorRate_CheckedChanged;
            }
        }

        async void Button_GsensorApply_Clicked(object sender, EventArgs e)
        {
            ResetAllFlags();

            await DoRateSet();
        }
        #endregion
    }
}
